package com.yyh.synctool.client.job;

import com.yyh.common.Common;
import com.yyh.synctool.client.component.TaskHelper;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

import java.util.Map;

/**
 * 给Job子类继承
 * 用于复用代码
 * @author xuan
 * @date 2021/12/16
 */
@Slf4j
public abstract class MyJob
{
    /** 任务id */
    protected abstract String getTaskId();

    protected void executeTask(JobExecutionContext jobExecutionContext) {
        String execTime = Common.currentTime();

        JobDataMap dataMap = jobExecutionContext.getMergedJobDataMap();
        TaskHelper taskHelper = (TaskHelper) dataMap.get("taskHelper");
        Map<String, TaskInfo> infos = (Map<String, TaskInfo>) dataMap.get("infos");
        // 当前任务信息
        TaskInfo info = infos.get(getTaskId());

        try {
            taskHelper.execute(getTaskId());

            // 任务可用
            info.setAvailable(true);
            info.setException(null);

        } catch (Exception e) {
            // 任务不可用
            log.error("error -->{}", e.getCause());
            info.setAvailable(false);
            info.setException(e);
        }

        info.setExecTime(execTime);
    }
}
